//
//  main.m
//  mainuiapp
//
//  Created by com on 2021/10/14.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

#include <iostream>
using namespace std;

#include <dlcom/cominc.h>
#include <dlcom/objectrun.hpp>
#include "appviewimpl.h"


/*
 
 iOS < 13
 mainuiapp[873:580234] [Application] The app delegate must implement the window property if it wants to use a main storyboard file
 
 */

int RunContainer(int argc, char * argv[], const char* path) {
	
	_pinstance hInstance = getpid();
	
	string container_framework_path;
	container_framework_path.append(path);

	HRESULT hr = S_OK;
	

	const char* pCode = "{\"component\":[ \
		  \"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:1:4\",\
		  \"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:2:4\",\
		  \"{201409F6-22F8-48d3-A69F-7935BDDE6BFF}:msgbus:10:4\" \
		  ]}";

	  /*
		\"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:5:4\",\
		\"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:4:4\",\
		\"{A8B9F69C-3523-406A-831B-2416421F324E}:logs:10:4\",\
		\"{51FA8DE1-216F-4A76-B4F4-B986E9F54C27}:mainview:100:4\",\
		\"{CE065148-7803-45B1-B1D7-874B52B8F5C5}:uibase:10:4\",\
	   */

	CContainer com;

	logi("test_main_CreateContainer");
	hr = com.CreateContainer(container_framework_path.c_str(), container_framework_path.length());

	logi("test_main_RegisterContainer");
	hr = com.RegisterContainer(pCode, s_strlen(pCode));

	logi("test_main_InitContainer");
	hr = com.InitContainer(hInstance, argv, argc);

	ULONG count = com.GetCompentCount();
	ULONG Index = com.GetStartIndex(count - 1);

	CAppViewImpl *app = ALLOC_NEW CAppViewImpl();
	_lComPtr<IBase> pBase;
	hr = app->QueryInterface(IID_IBase, (void**)&pBase);


	if (hr == S_OK) {
		hr = com.RegisterComponent(CLSID_AppView, "AppView.impl.V1", pBase, Index - 1, "main.exe", 	STD_COMPOENT | STD_INIT);
	}

	logi("test_main_RunContainer");
	hr = com.RunContainer(hInstance, argv, argc, 0);

	logi("test_main_InitContainer");
	hr = com.UnInitContainer(hInstance);
	
	return S_SUCCESS;
}

int main(int argc, char * argv[]) {
    
    NSString * appDelegateClassName;
    
    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
    }
    

    NSString * frameworkPath = [[NSBundle mainBundle] privateFrameworksPath];
    const char * path = [frameworkPath UTF8String];

	RunContainer(argc, argv, path);
    
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
